package com.landian.commons.jpa.builder;

import javassist.Modifier;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

import javax.persistence.Transient;
import java.lang.reflect.Field;

/**
 * @author caojl
 */
public class PredicateFactoryGuider {

	private static final Logger logger = Logger.getLogger(PredicateFactoryGuider.class);

	public static boolean needToBuildCriterionField(Field field,String fieldName){
		//0.忽略透明注解属性
		Transient transientAnnotations = field.getAnnotation(Transient.class);
		if(null != transientAnnotations){
			return false;
		}
		if(PredicateFactoryGuider.isIgnoreField(field, fieldName)){
			return false;
		}
		return true;
	}

	/**
	 * 由于基本类型存在默认值(不好识别是否为空)，笔者决定在设计实现上不解释基本类型
	 * @param field
	 * @param fieldName
	 * @return
	 */
	public static boolean isIgnoreField(Field field,String fieldName) {
		// 过滤serialVersionUID此属性
		String serialVersionUID = "serialVersionUID";
		if(serialVersionUID.equals(fieldName)) {
			return true;
		}
		//过滤静态属性
		if(Modifier.isStatic(field.getModifiers())) {
			return true;
		}
		if(field.getType() == int.class || field.getType() == long.class ||
				field.getType() == double.class || field.getType() == short.class || 
				field.getType() == byte.class || field.getType() == float.class || 
				field.getType() == boolean.class || field.getType() == char.class){
			return true;
		}
		return false;
	}

	/**
	 * 将字符串第一个字符变为小写
	 * @param str
	 * @return
	 */
	public static String lowerFirstChar(String str){
		if(StringUtils.isBlank(str)){
			return "";
		}
		return str.substring(0,1).toLowerCase() + str.substring(1,str.length());
	}
}
